package com.example.management.system.controller;

import com.example.management.common.domain.UserCompanyDO;
import com.example.management.common.utils.PageUtils;
import com.example.management.common.utils.Query;
import com.example.management.common.utils.R;
import com.example.management.system.service.UserCompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/userCompany")
public class UserCompanyController {
    @Autowired
    private UserCompanyService userCompanyService;

    @GetMapping()
    @RequiresPermissions("system:userCompany:userCompany")
    String UserCompany(){
        return "system/userCompany/userCompany";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("system:userCompany:userCompany")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<UserCompanyDO> userCompanyList = userCompanyService.list(query);
        int total = userCompanyService.count(query);
        PageUtils pageUtils = new PageUtils(userCompanyList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("system:userCompany:add")
    String add(){
        return "system/userCompany/add";
    }

    @GetMapping("/edit/{companyId}")
    @RequiresPermissions("system:userCompany:edit")
    String edit(@PathVariable("companyId") Long companyId, Model model){
        UserCompanyDO userCompany = userCompanyService.get(companyId);
        model.addAttribute("userCompany", userCompany);
        return "system/userCompany/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:userCompany:add")
    public R save( UserCompanyDO userCompany){
        if(userCompanyService.save(userCompany)>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("system:userCompany:edit")
    public R update( UserCompanyDO userCompany){
        userCompanyService.update(userCompany);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("system:userCompany:remove")
    public R remove( Long companyId){
        if(userCompanyService.remove(companyId)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("system:userCompany:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] companyIds){
        userCompanyService.batchRemove(companyIds);
        return R.ok();
    }

}